package de.lulusoft.anothertorrent.gui.commands;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.PlatformUI;

import de.lulusoft.anothertorrent.core.servers.Server;
import de.lulusoft.anothertorrent.gui.exceptions.ExceptionHandler;
import de.lulusoft.anothertorrent.gui.views.ServerView;
import de.lulusoft.anothertorrent.gui.wizards.NewServerWizard;

public class EditServerCommand implements IHandler, PropertyChangeListener {
	
	public static final String ID = "de.lulusoft.anothertorrent.gui.commands.EditServerCommand";

	private ArrayList<IHandlerListener> listeners = new ArrayList<IHandlerListener>();
	private boolean registeredWithServerView = false;
	private ServerView serverView = null;
	
	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		listeners.add(handlerListener);
	}

	@Override
	public void dispose() {
		
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		if (serverView  != null
				&& !serverView.getViewerSelection().isEmpty()
				&& serverView.getViewerSelection().getFirstElement() instanceof Server)
		{
			Server server = (Server)serverView.getViewerSelection().getFirstElement();
			// Show add server wizard
			NewServerWizard wizard = new NewServerWizard();
			wizard.setInput(server);
			WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getShell(), wizard);
			dialog.open();
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		try 
		{
			if (serverView == null)
				serverView = (ServerView)PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage().findView(ServerView.ID);
			if (!registeredWithServerView && serverView != null)
			{
				serverView.addPropertyChangeListener(this);
				registeredWithServerView = true;
			}
			
			if (serverView != null
					&& !serverView.getViewerSelection().isEmpty()
					&& serverView.getViewerSelection().getFirstElement()
					instanceof Server)
				return true;
		}catch (NullPointerException ne) {
			// ignore silently
		} 
		catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		return false;
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		listeners.remove(handlerListener);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getSource() instanceof ServerView
				&& evt.getPropertyName().equals(ServerView.P_SERVER))
		{
			for (IHandlerListener listener : listeners)
			{
				HandlerEvent handlerEvent = new HandlerEvent(this, true, false);
				listener.handlerChanged(handlerEvent );
			}
		}
	}

}
